package guiLayer;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class Creater extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected JTextField txtItem1;
	protected JTextField txtItem2;
	protected JTextField txtItem3;
	protected JLabel lblItem1;
	protected JLabel lblItem2;
	protected JLabel lblItem3;
	protected JPanel panel_2;
	protected JButton btnClear;
	protected JButton btnCreate;
	protected JPanel panel;
	protected JPanel panel_1;
	protected JLabel lblTitle;
	protected JLabel lblErrorItem1;
	protected JLabel lblErrorItem2;
	protected JLabel lblErrorItem3;
	protected JLabel lblCreated;
	protected JComboBox<String> cmb;
	protected JLabel lblCmb;
	
	/**
	 * Create the panel.
	 */
	public Creater() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{458, 183, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 0, 5);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 0;
		add(panel_1, gbc_panel_1);
		panel_1.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("max(40dlu;min)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("100dlu"),},
			new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		lblTitle = new JLabel("Title");
		lblTitle.setFont(new Font("SansSerif", Font.BOLD, 13));
		panel_1.add(lblTitle, "1, 1, 3, 1, center, default");
		
		lblCmb = new JLabel("cmb1");
		panel_1.add(lblCmb, "1, 3, left, default");
		
		cmb = new JComboBox<String>();
		panel_1.add(cmb, "3, 3, fill, default");
		
		lblItem1 = new JLabel("Item1");
		panel_1.add(lblItem1, "1, 5, left, default");
		
		txtItem1 = new JTextField();
		panel_1.add(txtItem1, "3, 5, fill, default");
		txtItem1.setColumns(10);
		
		lblErrorItem1 = new JLabel("");
		panel_1.add(lblErrorItem1, "5, 5");
		
		lblItem2 = new JLabel("Item2");
		panel_1.add(lblItem2, "1, 7, left, default");
		
		txtItem2 = new JTextField();
		panel_1.add(txtItem2, "3, 7, fill, default");
		txtItem2.setColumns(10);
		
		lblErrorItem2 = new JLabel("");
		panel_1.add(lblErrorItem2, "5, 7");
		
		lblItem3 = new JLabel("Item3");
		panel_1.add(lblItem3, "1, 9, left, default");
		
		txtItem3 = new JTextField();
		panel_1.add(txtItem3, "3, 9, fill, default");
		txtItem3.setColumns(10);
		
		lblErrorItem3 = new JLabel("");
		panel_1.add(lblErrorItem3, "5, 9");
		
		lblCreated = new JLabel("");
		lblCreated.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblCreated, "1, 11, 3, 1");
		
		panel_2 = new JPanel();
		panel_1.add(panel_2, "1, 13, 3, 1, fill, fill");
		panel_2.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.GROWING_BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.GROWING_BUTTON_COLSPEC,},
			new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,}));
		
		btnClear = new JButton("Clear");
		panel_2.add(btnClear, "2, 1");
		
		btnCreate = new JButton("Create");
		panel_2.add(btnCreate, "4, 1");
		
		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		
	}
	
	protected void error(final JLabel lbl, final String errorMsg){
		Thread errorThread = new Thread(){
			public void run(){
				lbl.setForeground(Color.RED);
				lbl.setText(errorMsg);
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				lbl.setText("");
			}
		};
		
		errorThread.start();
	}
	
	protected void created(final JLabel lbl, final String msg){
		Thread createdThread = new Thread(){
			public void run(){
				lbl.setForeground(Color.GREEN);
				lbl.setText(msg);
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				lbl.setText("");
			}
		};
		createdThread.start();
	}
	
	protected void clear() {
		txtItem1.setText("");
		txtItem2.setText("");
		txtItem3.setText("");
		lblErrorItem1.setText("");
		lblErrorItem2.setText("");
		lblErrorItem3.setText("");
	}
	
}
